package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Autowired
    private UserAccountService userAccountService;


    @Override
    public RechargeInfo getRechargeInfo(String orderNo) {
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(
                new LambdaQueryWrapper<RechargeInfo>()
                        .eq(RechargeInfo::getOrderNo, orderNo)
        );
        return rechargeInfo;
    }

    @Override
    public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        //1.创建充值记录对象
        RechargeInfo rechargeInfo = new RechargeInfo();
        rechargeInfo.setUserId(AuthContextHolder.getUserId());
        //2.生成充值订单编号 规则："cz"+雪花算法
        String orderNo = "cz" + IdUtil.getSnowflakeNextId();
        rechargeInfo.setOrderNo(orderNo);
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        //3.保存充值记录
        int insert = rechargeInfoMapper.insert(rechargeInfo);
        if (insert <= 0) {
            throw new RuntimeException("保存充值记录失败");
        }
        return Map.of("orderNo", orderNo);
    }

    @Override
    public void rechargePaySuccess(String orderNo) {
        //1.根据订单编号查询充值记录
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(
                new LambdaQueryWrapper<RechargeInfo>()
                        .eq(RechargeInfo::getOrderNo, orderNo)
        );
        if (rechargeInfo == null) {
            throw new GuiguException(500, "充值记录不存在");
        }
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
        rechargeInfoMapper.updateById(rechargeInfo);
        BigDecimal rechargeAmount = rechargeInfo.getRechargeAmount();
        //2.更新充值或者消费的记录
        userAccountService.update(null,
                new LambdaUpdateWrapper<UserAccount>()
                        .eq(UserAccount::getUserId, rechargeInfo.getUserId())
                        .setSql("total_amount = total_amount + " + rechargeAmount)
                        .setSql("total_income_amount = total_income_amount + " + rechargeAmount)
                        .setSql("available_amount = available_amount + " + rechargeAmount)
        );
        userAccountService.updateUserAccount(rechargeInfo.getUserId(), "充值", rechargeAmount, orderNo);
    }
}
